<?php

// @formatter:off
/**
 * @file tools.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: tools.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/services/tags/tools.php $
 */
// @formatter:on

function tools_createRequiredTags($requiredTags, WCGroup &$group) {
	$tags = array();

	foreach($requiredTags as $name) {
		$tag = WCTagsHolder::I()->createInGroup($name, $group);
		if($tag) {
			$tags[$tag->id] = $tag;
		}
	}

	return $tags;
}
function tools_filterTags(&$tags, $filter, $complement = true) {
	if($complement) {
		foreach($filter as $tag) {
			if(isset($tags[$tag->id])) {
				unset($tags[$tag->id]);
			}
		}
	} else {
		$currentTags = array();
		foreach($filter as $tag) {
			$currentTags[] = $tag->id;
		}
		foreach($tags as $key => $tag) {
			if(!in_array($key, $currentTags)) {
				unset($tags[$tag->id]);
			}
		}
	}
}
function tools_modeList($list, WCGroup &$group) {
	$tags = array();
	$unknownNames = array();
	$requiredTags = array();
	//
	// Transforming the list of tag names into a list of WCTag
	// objects.
	// If some of them doesn't seem to exists their are stored
	// for further checks into '$unknownNames'.

	foreach($list as $name) {
		$name = trim($name);
		if($name) {
			$tag = WCTagsHolder::I()->itemByNameInGroup($name, $group);
			$tag = array_shift($tag);
			if($tag) {
				$tags[$tag->id] = $tag;
			} else {
				$unknownNames[] = $name;
			}
		}
	}
	//
	// Trying to get tags with insensitive case.
	// If they don't exists, they are stored for further
	// creation in '$requiredTags'.
	foreach($unknownNames as $name) {
		$tag = WCTagsHolder::I()->itemByNameInGroupCI($name, $group);
		$tag = array_shift($tag);
		if($tag) {
			$tags[$tag->id] = $tag;
		} else {
			$requiredTags[] = $name;
		}
	}

	return array(
		"tags" => $tags,
		"required" => $requiredTags
	);
}
function tools_tagType($name) {
	$out = false;

	$pieces = explode(":", $name);
	if(count($pieces) > 1) {
		$out = $pieces[0];
	}

	return $out;
}
?>